(function ($, undefined) {
    $(document).ready(function () {
        var flag4 = 1;
        var _t = 60; // 倒计时时间
        var subFlag = "1";
        var flag = 0;
        var wait = 300;
        var verify1 = "";
        var verify2 = "";
        var flag1 = false;
        var flaghave = 0;
        var flag3 = 1;
        var $invist = $("._invist");
        var $invist_msg = $("._invist_msg");
        var $getKey = $("._getkey");
        var $phoneyanzhengma = $("._yanzhengma");
        // var $yanzhengma = $("#jpgVerify");
        // var $yanzhengmatishi = $('#jpgVerifys');
        var invist_flag = true;
        var $changeCapcherButton = $("._changeCapcherButton");
        var $phoneMsg = $('#phoneJy');
        var phones = 1;
        var names = 1;
        var passwordss = 1;
        var yzms = 1;
        var dxyzs = 1;
        var tjrs = 1;
        var emails = 1;
        var login = {
            init: function () {
                login._bind();
            },
            _bind: function () {
                $invist.on('blur', function (event) {
                    event.preventDefault();
                    login.validateInvist();
                    return false;
                });
                $getKey.on('click', function (event) {
                    event.preventDefault();
                    if (flag3 != 0) {
                        $("#phoneJy").text("");
                        $("#phoneJy").append("<span style=color:#ff7800>请先输入正确的验证码</span>");
                        return false;
                    }
                    if (flag4 == 1) {
                        flag4 = 0;
                        login._ya($(this));
                    }
                    return false;
                });
                $("._phoneNum").on('blur', function (event) {
                    event.preventDefault();
                    login.phoneYz();
                    return false;
                });
                $("._phonVerify").on('blur', function (event) {
                    event.preventDefault();
                    login.checkSecurity($(this));
                    return false;
                });
                $("._userName").on('blur', function (event) {
                    event.preventDefault();
                    login.strVerify($(this));
                    return false;
                });
                $("._password").on('blur', function (event) {
                    event.preventDefault();
                    login.strVerify($(this));
                    return false;
                });
                $("._repeatPassword").on('blur', function (event) {
                    event.preventDefault();
                    login.strVerify($(this));
                    return false;
                });
                $("._userEmail").on('blur', function (event) {
                    event.preventDefault();
                    login.strVerify($(this));
                    return false;
                });
                $phoneyanzhengma.on('blur', function (event) {
                    event.preventDefault();
                    login.verify($(this));
                    return false;
                });
                $("._ajaxSubmit").on('click', function (event) {
                    event.preventDefault();
                    login.ajaxSubmit();
                    return false;
                });
            },
            _ya: function (o) {
                if (login.phoneSend(o)) {
                    if (flaghave != "1") {
                        login._daojishi();
                    }
                } else {
                    flag4 = 1;
                }
            },
            phoneYz: function () { // 手机号验证
                var utel = $("#phone");
                var str = utel.val();
                var regPartton = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
                if (str.length > 11) {
                    $("#phoneJy").text("");
                    $("#phoneJy").append("<span style=color:#ff7800>手机号格式不正确</span>");
                    phones = 1;
                    flag = 1;
                    return false;
                } else if (!str || str == null) {
                    $("#phoneJy").text("");
                    $("#phoneJy").append("<span style=color:#ff7800>手机号不可为空</span>");
                    phones = 1;
                    flag = 1;
                    return false;
                } else if (!regPartton.test(str)) {
                    $("#phoneJy").text("");
                    $("#phoneJy").append("<span style=color:#ff7800>手机号格式不正确</span>");
                    phones = 1;
                    flag = 1;
                    return false;
                } else {
                    phones = 0;
                    flag = 0;
                    login.phoneSend($getKey);
                    return true;
                }
            },
            phoneSend: function (o) {
                if (flag == "1") {
                    return false;
                }
                var isSuccess = false;
                $.ajax({
                    type: "post", //请求方式
                    dataType: "json",
                    url: "/erb/loan/reglog/dxyzm", //发送请求地址
                    async: false,
                    data: { //发送给数据库的数据
                        phone: $("#phone").val(),
                    },
                    //请求成功后的回调函数有两个参数
                    success: function (data) {
                        flag4 = 1;
                        if (data == 1) { //号码存在时
                            wait = 300;
                            flaghave = 1;
                            $phoneMsg.text("");
                            $phoneMsg.html("<span style=color:#ff7800>该手机号码已经注册</span>");
                        } else {
                            phones = 0;
                            flag4 = 0;
                            isSuccess = true;
                            $phoneMsg.text("");
                        }
                    },
                    error: function (data, textStatus) {
                        flag4 = 1;
                    }
                });
                if (isSuccess) {
                    login._changetCapther();
                    o.val("重新发送(" + wait + ")");
                    wait--;
                    return true;
                } else {
                    return false;
                }
            },
            checkSecurity: function (vr) {
                var byName = vr.data('_id');  //短信验证码输入框的值
                var ids = '#' + byName;
                $.ajax({
                    type: "post", //请求方式
                    dataType: "json",
                    url: "/erb/user/yzdxyzm", //发送请求地址
                    data: { //发送给数据库的数据
                        code: vr.val(),
                    },
                    //请求成功后的回调函数有两个参数
                    success: function (data) {
                        $(ids + "s").text("");
                        if (data == 0) {
                            $(ids + "s").html("<span style=color:green>验证成功</span>");
                            dxyzs = 0;
                            flag1 = true;
                            if (byName == "phonVerify") {
                                verify1 = "1";
                            } else {
                                verify2 = "1";
                            }
                        } else {
                            $(ids + "s").html("<span style=color:#ff7800>验证失败</span>");
                            dxyzs = 1;
                            flag1 = false;
                            if (byName == "phonVerify") {
                                verify1 = "2";
                            } else {
                                verify2 = "2";
                            }
                        }
                    },
                    error: function (data, textStatus) {
                    }
                });
            },
            strVerify: function (event) {
                var strName = event.attr('id');  //获取去指定的属性值：userName、password、repeatPassword、userEmail
                var strVal = event.val();  //输入框的值
                var ids = '#' + strName + 'Alt';
                //验证用户名
                if (strName == 'userName') {
                    if (strVal == null || strVal == '') {
                        names = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>用户名不能为空</span>");
                        return false;
                    } else if (!(/^[a-zA-Z][a-zA-Z0-9_]{6,15}$/).test(strVal)) {
                        names = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>用户名只能为以字母开头,字母、数字下划线组成</span>");
                        return false;
                    } else if (strVal.length < 6 || strVal.length > 16) {
                        names = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>用户名为6-16位字符</span>");
                        return false;
                    } else {
                        $.ajax({
                            type: "post",
                            dataType: "json",
                            url: "/erb/loan/reglog/seluser", //发送请求地址
                            async: false,
                            data: {
                                name: strVal
                            },
                            //请求成功后的回调函数有两个参数
                            success: function (data) {
                                if (data.msg == 0) {
                                    names = 0;
                                    $(ids).text("");
                                    $(ids).append("<span style=color:green>填入信息可用</span>")
                                } else {
                                    names = 1;
                                    $(ids).text("");
                                    $(ids).append("<span style=color:#ff7800>用户名已存在</span>")
                                }
                            }
                        });
                    }
                }
                //验证密码
                if (strName == 'password') {
                    if (strVal == null || strVal == '') {
                        passwordss = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>密码不能为空</span>");
                    } else if (strVal.length < 6 || strVal.length > 15) {
                        passwordss = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>密码为6-24位字符</span>");
                    } else if ((/[^0-9a-zA-Z]/g).test(strVal)) {
                        passwordss = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>密码必须是数字和字符组合</span>");
                    } else {
                        $(ids).text("");
                        $(ids).append("<span style=color:green>填入信息可用</span>");
                    }
                }
                //重复密码
                if (strName == 'repeatPassword') {
                    if ($("#repeatPassword").val() != $("#password").val()) {
                        passwordss = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>两次输入密码不一样</span>");
                    } else {
                        passwordss = 0;
                        $(ids).text("");
                        $(ids).append("<span style=color:green>密码输入一致</span>");
                    }
                }
                //验证邮箱
                if (strName == 'userEmail') {
                    var atpos = strVal.indexOf("@");
                    var dotpos = strVal.lastIndexOf(".");
                    if (strVal == null || strVal == '') {
                        emails = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>邮箱不能为空</span>");
                    } else if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= strVal.length) {
                        emails = 1;
                        $(ids).text("");
                        $(ids).append("<span style=color:#ff7800>不是一个有效的邮箱地址</span>");
                    } else {
                        $.ajax({
                            type: "post",
                            dataType: "json",
                            url: "/erb/loan/reglog/selusers", //发送请求地址
                            async: false,
                            data: {
                                email: strVal
                            },
                            //请求成功后的回调函数有两个参数
                            success: function (data) {
                                if (data.msg == 0) {
                                    emails = 0;
                                    $(ids).text("");
                                    $(ids).append("<span style=color:green>邮箱可用</span>");
                                } else {
                                    emails = 1;
                                    $(ids).text("");
                                    $(ids).append("<span style=color:#ff7800>邮箱已绑定，请更换一个邮箱</span>")
                                }
                            }
                        });

                    }
                }
                //结束
            },
            verify: function (vr) {
                var byName = vr.attr("id");
                var ids = '#' + byName;
                $.ajax({
                    type: "post", //请求方式
                    dataType: "json",
                    async: false,
                    url: "/erb/user/valid", //发送请求地址
                    data: { //发送给数据库的数据
                        code: $(ids).val(),
                    },
                    success: function (data) {
                        if (data == 0) {
                            $(ids + "s").text("");
                            $(ids + "s").append("<span style=color:green>验证成功</span>");
                            yzms = 0;
                            flag3 = 0;
                            if (byName == "phonVerify") {
                                verify1 = "1";
                            } else {
                                verify2 = "1";
                            }
                        } else {
                            $(ids + "s").text("");
                            $(ids + "s").append("<span style=color:#ff7800>验证码错误</span>");
                            yzms = 1;
                            flag3 = 1;
                            if (byName == "phonVerify") {
                                verify1 = "2";
                            } else {
                                verify2 = "2";
                            }
                        }
                    },
                    error: function (data, textStatus) {
                        console.log(textStatus);
                    }
                });
            },

            ajaxSubmit: function () {
                var selectedItems = [];
                $("input[name='protocol']:checked").each(function () {
                    selectedItems.push($(this).val());
                });
                if (names == 0 && phones == 0 && passwordss == 0 && yzms == 0 && dxyzs == 0 && emails == 0) {
                    var name = $('#userName').val().trim();
                    var password = $('#password').val().trim();
                    var phone = $("#phone").val().trim();
                    var email = $("#userEmail").val().trim();
                    var sex = $("#sex").val();
                    location.href = "/erb/loan/reglog/add?name=" + name + "&password=" + password + "&phone=" + phone + "&email=" + email + "&sex=" + sex;
                    subFlag = "2";
                } else {
                    let _i = layer.msg('请继续完善信息！', {time: 1000});
                    layer.style(_i, {
                        'background-color': 'lightgrey'
                    });
                }

            },
            _daojishi: function () {
                login._setti(_t);
            },
            _setti: function (i) {
                setTimeout(function () {
                    if (i == 0) {
                        $getKey.html("获取验证码");
                        flag4 = 1;
                    } else {
                        $getKey.html("重新发送(" + i + ")");
                        login._setti(parseInt(i - 1));
                    }
                }, 1000);
            },
            validateInvist: function () {
                $.ajax({
                    url: '/erb/user/seluser',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name: $invist.val()
                    },
                    success: function (result) {
                        if (result.msg == 1) {
                            $invist_msg.css("color", "green");
                            $invist_msg.html("推荐人正确");
                            tjrs = 0;
                            invist_flag = true;
                        } else {
                            $invist_msg.css("color", "#ff7800");
                            $invist_msg.html("推荐人不存在!");
                            invist_flag = false;
                        }
                    },
                    error: function () {
                        layer.msg('服务器错误，请稍后再试！');
                    }
                });
            },
            _changetCapther: function () {
                $changeCapcherButton.trigger('click');
                flag3 = 1;
                return false;
            }
        };
        login.init();
    });
})(jQuery);